/**
 * Created by lvzj on 15/11/23.
 */

var communityPrice = 40000;
var wxUrl = "http://weixin-66xiaoqu.babytree.com/"
var shareData = {};
shareData.imgUrl = resUrl + "images/eleven/share_pic.jpg";
shareData.link = htmlUrl + "eleven/insite.html?1=1";
shareData.desc = "剁手党忏悔吧！揭开双十一背后隐藏的花费！";
shareData.title = "听说11.11你抢了不少？偷偷告诉你，你亏大了！";

$('#top').hide();

if (params.token) {
    //app内的流程
    //$('.page3-bottom').html($('.page3-bottom').html() + '<p class="page3-p5">赶快去发闲置吧！</p>');

    //$.ajax({
    //    type: "GET",
    //    url: serverUrl + '/rest/v1.0/eleven/getUserInfo/' + params.token + '/' + params.deviceId,
    //    success: function (data) {
    //        if (data.data && data.data.community &&  data.data.community.price) {
    //            communityPrice = data.data.community.price;
    //        }
    //    }
    //});

    $('#susu').html('赶紧上66二手集市');
    $('#shareContent').html('');

} else if (params.openId) {
    //微信流程
    //$('.page3-bottom').html($('.page3-bottom').html() + '<a href="javascript:download();" class="page3-a">立即下载</a>');

    wx.ready(function () {
        wx.onMenuShareAppMessage({
            title: shareData.title,
            link: shareData.link,
            imgUrl: shareData.imgUrl,
            desc:shareData.desc,
            success: function () {
                //showResult();
                clickAcquisition({type: 59, typeDesc: '(双11游戏)微信分享成功'});
            }
        });
        wx.onMenuShareTimeline({
            title: shareData.title,
            link: shareData.link,
            imgUrl: shareData.imgUrl,
            success: function () {
                //showResult();
                clickAcquisition({type: 59, typeDesc: '(双11游戏)微信分享成功'});
            }
        });
        wx.onMenuShareQQ(shareData);
        wx.onMenuShareWeibo(shareData);
    });

    $.post('http://weixin-66xiaoqu.babytree.com/wxJs/getWxJsConfig.do', function (json) {
        timestamp = json.data.timestamp;
        appId = json.data.appId;
        nonceStr = json.data.nonceStr;
        signature = json.data.signature;
        share(appId, timestamp, nonceStr, signature);
    }, 'json');

    function share(appId, timestamp, nonceStr, signature) {
        wx.config({
            debug: false,
            appId: appId,
            timestamp: timestamp,      //生成签名的时间戳
            nonceStr: nonceStr,       //生成签名的随机串
            signature: signature,     //签名
            jsApiList: [
                'checkJsApi',
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                'onMenuShareQQ',
                'onMenuShareWeibo'
            ]
        });
    }
} else {
    //默认站外，跳转至微信流程
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
        location.href = wxUrl + "wx/auth.do?backUrl=" + shareData.link;
    }
}
var area = 0;
var price = 0;
var desc = "";
var baifenbi = "";

//显示分享页
$('.pop .btn').click(function () {
    //alert($('#communityName').val())
    if(!$('#communityName').val()){
        return ;
    }

    clickAcquisition({type: 60, typeDesc: '(双11游戏)结果页面查看'});

    if((area+"").indexOf('.') > 0){
        $('#area').html((area+"").split('.')[0]+"."+(area+"").split('.')[1].substring(0,2));
    }else{
        $('#area').html(area);
    }

    $('.page1').hide();
    $('.page3').show();
    $('#wrapper').html('<span id="top"><a href="javascript:download();" class="sharepage-top"><img src="'+resUrl+'images/share/share_top.jpg"></a></span>'+$('#wrapper').html());

    clickAcquisition({type: 58, typeDesc: '(双11游戏)输入小区名后点击下一步', ext: $('.pop .text').val()});
});

$('.page1-next').click(function () {

    if (params.token) {
        $('.page1').hide();
        $('.page2').show();
    } else {
        $('.pop').show();
    }

    $('.goods-box a').each(function (i) {
        area += $(this).data('counts') * $(this).data('area')
    });

    price = area * communityPrice;

    clickAcquisition({type: 57, typeDesc: '(双11游戏)选择产品点击下一步'});

    statisticText();

});


//显示分享结果页
$('.share-btn').click(function () {
    clickShareBtn();
});

function showResult() {

    clickAcquisition({type: 60, typeDesc: '(双11游戏)结果页面查看'});

    if((area+"").indexOf('.') > 0){
        $('#area').html((area+"").split('.')[0]+"."+(area+"").split('.')[1].substring(0,2));
    }else{
        $('#area').html(area);
    }

    $('.page2').hide();
    $('.page3').show();


}

function download() {

    clickAcquisition({type: 61, typeDesc: '(双11游戏)点击下载按钮'});

    toDownload();

}

function clickShareBtn() {

    clickAcquisition({type: 59, typeDesc: '(双11游戏)点击分享按钮'});

    if (params.token) {
        if (params.type == 'ios') {
            document.location.href = "66xiaoqu:market:share##title=" + shareData.title + "&nbsp;desc=" + shareData.desc + "&nbsp;image=" + shareData.imgUrl + "&nbsp;url=" + shareData.link;
        } else {
            webviewhandle.shareGoodsDetail(shareData.title, shareData.desc, shareData.imgUrl, shareData.link);//webviewhandle为webview注册对象
        }

        setTimeout("getShareInfo()", 5000);
        //getShareInfo()
    }
}

function getShareInfo() {

    //$.ajax({
    //    type: "GET",
    //    url: serverUrl+'/rest/v1.0/mobile/shares/getShareRestfulByCondition/'+params.token,
    //    data: {},
    //    dataType: "json",
    //    success: function(data){
    //        if(data.data){
    showResult();
    //        }
    //    }
    //});

}

function statisticText() {
    price = parseInt(price);

    if(price == 0){
        //$('.page3-p3').html('');
        $('#fangjia').html('非常好')
        desc = "您抵住了买买买的诱惑";
        baifenbi = '0%';
        shareData.title = "别以为你赚了便宜，揭开双十一背后隐藏的花费！";
        shareData.desc = "剁手党忏悔吧！你肯定没抵住买买买的诱惑！";
        //shareData.title = "我竟然击败了5%的上海宁，扔掉了1顿米其林大餐！";
    } else if (price <= 3000) {
        desc = "您扔掉了1顿米其林大餐！";
        baifenbi = '5%';
        shareData.title = "我竟然击败了5%的上海宁，扔掉了1顿米其林大餐！";
    } else if (price > 3000 && price <= 6000) {
        desc = "您扔掉了1顿顶级怀石料理！";
        baifenbi = '10%';
        shareData.title = "我竟然击败了10%的上海宁，扔掉了1顿顶级怀石料理！";
    } else if (price > 6000 && price <= 10000) {
        desc = "您扔掉了1顿饕餮满汉全席！";
        baifenbi = '15%';
        shareData.title = "我竟然击败了15%的上海宁，扔掉了1顿饕餮满汉全席！";
    } else if (price > 10000 && price <= 20000) {
        desc = "您放弃了1趟马尔代夫度假之旅！";
        baifenbi = '20%';
        shareData.title = "我竟然击败了20%的上海宁，放弃1趟马尔代夫度假之旅！";
    } else if (price > 20000 && price <= 40000) {
        desc = "您放弃了1次梦幻欧洲游！";
        baifenbi = '40%';
        shareData.title = "我竟然击败了40%的上海宁，放弃了1次梦幻欧洲游！";
    } else if (price > 40000 && price <= 60000) {
        desc = "您舍弃了一枚一克拉钻戒！";
        baifenbi = '70%';
        shareData.title = "我竟然击败了70%的上海宁，舍弃了一颗一克拉钻戒！";
    } else if (price > 60000 && price <= 80000) {
        desc = "您都能包天直升飞机了！";
        baifenbi = '80%';
        shareData.title = "我竟然击败了80%的上海宁，都能包天直升飞机了！";
    } else if (price >= 80000 && price <= 100000) {
        desc = "您都能住五星级酒店2个月了！";
        baifenbi = '90%';
        shareData.title = "我竟然击败了90%的上海宁，都能住五星级酒店2个月了！";
    } else if (price >= 100000) {
        desc = "您放弃了一辆全新汽车！";
        baifenbi = '99%';
        shareData.title = "我竟然击败了99%的上海宁，放弃了一辆全新汽车！";
    }

    wx.ready(function () {
        wx.onMenuShareAppMessage({
            title: shareData.title,
            link: shareData.link,
            imgUrl: shareData.imgUrl,
            desc:shareData.desc,
            success: function () {
                //showResult();
                clickAcquisition({type: 59, typeDesc: '(双11游戏)微信分享成功'});
            }
        });
        wx.onMenuShareTimeline({
            title: shareData.title,
            link: shareData.link,
            imgUrl: shareData.imgUrl,
            success: function () {
                //showResult();
                clickAcquisition({type: 59, typeDesc: '(双11游戏)微信分享成功'});
            }
        });
        wx.onMenuShareQQ(shareData);
        wx.onMenuShareWeibo(shareData);
    });

    $('#baifenbi').html(baifenbi);
    $('#desc').html(desc);
}